package com.tqjc.execute.crane.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.crane.entity.CraneInfo;
import com.tqjc.execute.crane.mapper.CraneInfoMapper;

import com.tqjc.execute.crane.service.CraneInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 天车信息服务实现
 * @author: pzy
 * @date: 2023年05月18日 9:41
 */

@Slf4j
@Service("craneInfo")
public class CraneInfoServiceImpl extends ServiceImpl<CraneInfoMapper,CraneInfo> implements CraneInfoService {
    @Resource
    private CraneInfoMapper craneInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CraneInfo queryById(Integer id){
        return craneInfoMapper.selectById(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param name 天车编号
     * @return 实例对象
     */
    @Override
    public CraneInfo queryByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);

        List<CraneInfo> craneInfos = craneInfoMapper.selectList(queryWrapper);
        return craneInfos.get(0);
    }

    /**
     * 分页查询
     *
     * @param craneInfo 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<CraneInfo> paginQuery(CraneInfo craneInfo, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<CraneInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(craneInfo.getName())){
            queryWrapper.eq(CraneInfo::getName, craneInfo.getName());
        }
        //2. 执行分页查询
        Page<CraneInfo> pagin = new Page<>(current , size , true);
        IPage<CraneInfo> selectResult = craneInfoMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param craneInfo 实例对象
     * @return 实例对象
     */
    @Override
    public CraneInfo insert(CraneInfo craneInfo){
        craneInfoMapper.insert(craneInfo);
        return craneInfo;
    }

    /**
     * 更新数据
     *
     * @param craneInfo 实例对象
     * @return 实例对象
     */
    @Override
    public CraneInfo update(CraneInfo craneInfo){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<CraneInfo> chainWrapper = new LambdaUpdateChainWrapper<CraneInfo>(craneInfoMapper);
        if(StrUtil.isNotBlank(craneInfo.getName())){
            chainWrapper.eq(CraneInfo::getName, craneInfo.getName());
        }
        //2. 设置主键，并更新
        chainWrapper.set(CraneInfo::getId, craneInfo.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(craneInfo.getId());
        }else{
            return craneInfo;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id){
        int total = craneInfoMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public Integer getRollerIdByCraneName(String craneType) {

        return craneInfoMapper.getRollerIdByCraneName(craneType);
    }

    @Override
    public void updateByName(String craneType, WorkStatus workStatus) {
        String msg = JSON.toJSONString(workStatus);
        String nowStatus = workStatus.getNowStatus();
        int status = "运行中".equals(nowStatus) ? 1:0;
        craneInfoMapper.update(null,new LambdaUpdateWrapper<CraneInfo>().eq(CraneInfo::getName,craneType)
                .set(CraneInfo::getDetailMsg,msg).set(CraneInfo::getWorkStatus,status));
    }
}
